package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import redis.clients.jedis.Jedis;

/**
 * 公司信息的服务逻辑层
 */
public class CompanyService {
    /**
     * 得到公司数据
     * 1.判断缓存里面是否有数据
     * 有：直接将缓存数据取出，赋值给CompanyModel
     * 没有：则取数据库数据，将数据库数据存入缓存
     *
     * @return
     */
    public CompanyModel getCompanyModel() {
        final CompanyModel[] companyModel = {null};
        for (int i = 0; i < 500; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    companyModel[0] = setData();
                }
            }).start();
        }
        return companyModel[0];
    }

    /**
     *
     * @return
     */
    private synchronized CompanyModel setData() {
        Jedis jedis = new Jedis();
        CompanyModel companyModel = new CompanyModel();
        boolean isHave = jedis.exists("company_id");
        if (isHave) {
//取緩存
            companyModel.setLogo(jedis.get("company_logo"));
            companyModel.setPhone(jedis.get("company_phone"));
            companyModel.setAd(jedis.get("company_ad"));
            companyModel.setNet(jedis.get("company_net"));
            companyModel.setEmail(jedis.get("company_email"));
            companyModel.setLinePhone(jedis.get("company_line_phone"));
            companyModel.setAddress(jedis.get("company_address"));
            companyModel.setVersion(jedis.get("company_version"));
            System.out.println("缓存");
        } else {
//取数据库值
//            将数据存入redis
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getModel();
//            jedis.set("company_id", companyModel.getId().toString());
//            jedis.expire("company_id", 30 * 60);
            jedis.setex("company_id", 30 * 60, companyModel.getId().toString());
            jedis.set("company_logo", companyModel.getLogo());
            jedis.set("company_phone", companyModel.getPhone());
            jedis.set("company_ad", companyModel.getAd());
            jedis.set("company_net", companyModel.getNet());
            jedis.set("company_email", companyModel.getEmail());
            jedis.set("company_line_phone", companyModel.getLinePhone());
            jedis.set("company_address", companyModel.getAddress());
            jedis.set("company_version", companyModel.getVersion());
            System.out.println("数据库");
        }
        return companyModel;
    }

    public int update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.update(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("company_id");
        }
        return result;
    }

    public int add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.update(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("company_id");
        }
        return result;
    }

    public int del(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.update(companyModel);
        if (result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("company_id");
            jedis.flushDB();
//            jedis.flushAll();
        }
        return result;
    }

}
